@php
    // 获取当前请求信息
    $request = request();
    $currentPath = $request->path();
    
    // 尝试从路径确定当前模块
    $pathSegments = explode('/', $currentPath);
    
    // 调整模块识别逻辑：Admin是主模块，其他模块在admin路径下
    $currentModuleName = 'Admin'; // 默认为Admin模块
    
    // 如果路径以admin开头，检查是否有子模块
    if (isset($pathSegments[0]) && strtolower($pathSegments[0]) === 'admin') {
        // 检查是否有子模块部分
        if (isset($pathSegments[1])) {
            // 尝试根据路径第二段确定模块
            $possibleModule = ucfirst($pathSegments[1]);
            
            // 模块列表(预加载)
            $moduleStatuses = json_decode(file_get_contents(base_path('modules_statuses.json')), true);
            
            // 检查识别的模块是否存在并启用
            if (isset($moduleStatuses[$possibleModule]) && $moduleStatuses[$possibleModule] === true) {
                $currentModuleName = $possibleModule;
            }
        }
    }
    
    // 获取所有模块信息
    $moduleStatuses = json_decode(file_get_contents(base_path('modules_statuses.json')), true);
    $enabledModules = array_filter($moduleStatuses, function($enabled) {
        return $enabled === true;
    });
    
    // 获取当前模块详细信息
    $modulesInfo = [];
    foreach(array_keys($enabledModules) as $moduleName) {
        $modulePath = base_path('Modules/' . $moduleName);
        $moduleJsonPath = $modulePath . '/module.json';
        
        if (file_exists($moduleJsonPath)) {
            $moduleData = json_decode(file_get_contents($moduleJsonPath), true);
            $modulesInfo[$moduleName] = $moduleData;
        }
    }
    
    // 当前模块信息
    $currentModuleInfo = $modulesInfo[$currentModuleName] ?? null;
    
    // 定义菜单项
    $menuItems = [
        [
            'title' => '控制台',
            'icon' => 'layui-icon-home',
            'url' => route('admin.index'),
            'active' => $currentPath === 'admin',
            'children' => []
        ],
        [
            'title' => '权限管理',
            'icon' => 'layui-icon-auz',
            'url' => '#',
            'active' => strpos($currentPath, 'admin/auth') === 0,
            'children' => [
                [
                    'title' => '管理员',
                    'url' => '#',
                    'active' => strpos($currentPath, 'admin/auth/admins') === 0,
                ],
                [
                    'title' => '角色管理',
                    'url' => route('admin.auth.roles.index'),
                    'active' => strpos($currentPath, 'admin/auth/roles') === 0,
                ],
                [
                    'title' => '权限管理',
                    'url' => route('admin.auth.permissions.index'),
                    'active' => strpos($currentPath, 'admin/auth/permissions') === 0,
                ]
            ]
        ],
        [
            'title' => '组件管理',
            'icon' => 'layui-icon-component',
            'url' => route('admin.components.index'),
            'active' => strpos($currentPath, 'admin/components') === 0,
            'children' => []
        ]
    ];
    
    /**
     * 递归渲染菜单
     */
    function renderMenu($items) {
        foreach ($items as $item) {
            if (count($item['children']) > 0) {
                echo '<li class="layui-nav-item '.($item['active'] ? 'layui-nav-itemed' : '').'">';
                echo '<a href="'.$item['url'].'" class="menu-link">';
                if (isset($item['icon'])) {
                    echo '<i class="layui-icon '.$item['icon'].' mr-2"></i>';
                }
                echo $item['title'].'</a>';
                echo '<dl class="layui-nav-child">';
                foreach ($item['children'] as $child) {
                    echo '<dd class="'.($child['active'] ? 'layui-this' : '').'">';
                    echo '<a href="'.$child['url'].'" class="menu-link">'.$child['title'].'</a>';
                    echo '</dd>';
                }
                echo '</dl>';
                echo '</li>';
            } else {
                echo '<li class="layui-nav-item '.($item['active'] ? 'layui-this' : '').'">';
                echo '<a href="'.$item['url'].'" class="menu-link">';
                if (isset($item['icon'])) {
                    echo '<i class="layui-icon '.$item['icon'].' mr-2"></i>';
                }
                echo $item['title'].'</a>';
                echo '</li>';
            }
        }
    }
@endphp

<!-- 模块标题 -->
<div class="px-4 py-3 border-b border-gray-700">
    <h2 class="text-xl font-semibold text-white">{{ $currentModuleInfo['alias'] ?? $currentModuleName }}</h2>
</div>

<!-- 菜单列表 -->
<div class="py-2">
    <ul class="layui-nav layui-nav-tree" lay-filter="adminMenu" lay-shrink="all">
        @php
            renderMenu($menuItems);
        @endphp
    </ul>
</div>

<style>
    .layui-nav-tree {
        width: 100%;
    }
    .layui-nav .layui-nav-item a {
        color: #f0f0f0;
    }
    .layui-nav-tree .layui-nav-child dd.layui-this, 
    .layui-nav-tree .layui-nav-child dd.layui-this a, 
    .layui-nav-tree .layui-this, 
    .layui-nav-tree .layui-this>a {
        background-color: #1E9FFF;
    }
    .layui-nav-tree .layui-nav-bar {
        background-color: #1E9FFF;
    }
</style>

<script>
    layui.use(['element'], function() {
        var element = layui.element;
    });
</script>


